# -*- coding: utf-8 -*-
#
# Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available.
# Copyright (C) 2017-2019 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://opensource.org/licenses/MIT
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
import re

from backend.utils.basic import ChoicesEnum

# 变量的格式
VARIABLE_PATTERN = "[A-Za-z][A-Za-z0-9_]"
# 填写数字的地方，可以填写变量
NUM_VAR_PATTERN = "^{{%s*}}$" % VARIABLE_PATTERN
# 需要与 backend/apps/variable/serializers.py 中的说明保持一致
NUM_VAR_ERROR_MSG = "只能包含字母、数字和下划线，且以字母开头"
# 文件目录正则
FILE_DIR_PATTERN = "^((?!\.{\$)[\w\d\-\.\/~{}\$]+)+$"

KEY_PATTERN = re.compile(r'{{([^}]*)}}')

REAL_NUM_VAR_PATTERN = re.compile(r"^%s*$" % VARIABLE_PATTERN)

# configmap/secret key 名称限制
KEY_NAME_PATTERN = "^[a-zA-Z{]{1}[a-zA-Z0-9-_.{}]{0,254}$"

# TODO mark refactor 这部分整个项目需要统一到一个地方维护
KRESOURCE_NAMES = ['K8sDeployment', 'K8sDaemonSet', 'K8sJob', 'K8sStatefulSet', 'K8sService', 'K8sConfigMap',
                   'K8sSecret', 'K8sIngress', 'K8sHPA']
MRESOURCE_NAMES = ['application', 'deployment', 'service', 'configmap', 'secret', 'hpa']
RESOURCE_NAMES = KRESOURCE_NAMES + MRESOURCE_NAMES


class TemplateCategory(ChoicesEnum):
    SYSTEM = 'sys'
    CUSTOM = 'custom'

    _choices_labels = (
        (SYSTEM, "系统"),
        (CUSTOM, "自定义")
    )


# TODO mark refactor 考虑用起来
class K8sResourceName(ChoicesEnum):
    K8sDeployment = 'K8sDeployment'
    K8sDaemonSet = 'K8sDaemonSet'
    K8sJob = 'K8sJob'
    K8sStatefulSet = 'K8sStatefulSet'
    K8sService = 'K8sService'
    K8sConfigMap = 'K8sConfigMap'
    K8sSecret = 'K8sSecret'
    K8sIngress = 'K8sIngress'
    K8sHPA = 'K8sHPA'

    _choices_labels = (
        (K8sDeployment, 'K8sDeployment'),
        (K8sDaemonSet, 'K8sDaemonSet'),
        (K8sJob, 'K8sJob'),
        (K8sStatefulSet, 'K8sStatefulSet'),
        (K8sService, 'K8sService'),
        (K8sConfigMap, 'K8sConfigMap'),
        (K8sSecret, 'K8sSecret'),
        (K8sIngress, 'K8sIngress'),
        (K8sHPA, 'K8sHPA'),
    )


class MesosResourceName(ChoicesEnum):
    application = 'application'
    deployment = 'deployment'
    service = 'service'
    configmap = 'configmap'
    secret = 'secret'
    hpa = 'hpa'

    _choices_labels = (
        (application, 'application'),
        (deployment, 'deployment'),
        (service, 'service'),
        (configmap, 'configmap'),
        (secret, 'secret'),
        (hpa, 'hpa')
    )
